package shellstation;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class UserController {

    private Connection conn = null;
    private Statement stmt;


    public void ConnectToDatabase(){
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "shellstation";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "12345";

        try{

            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url+dbName,userName,password);

        }

        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void insertUser(String ut,String u, String p, String cp, String sq, String sa){

        ResultSet rs;

        try{
            ConnectToDatabase();
            Statement stmt = conn.createStatement();
            
                  stmt.executeUpdate("Insert into account(userType, username, password, securityQuestion, securityAnswer) "
                    + "Values('"+ut+"','"+u+"','"+p+"','"+sq+"','"+sa+"')");

        }
        catch (Exception e){
            e.printStackTrace();
            }

        
    }
public int delteUser(String u){
        ResultSet rs;

        try{
            ConnectToDatabase();
            Statement stmt = conn.createStatement();


                  rs=stmt.executeQuery("Select userID from account where username = '"+u+"'");

                  if(rs.next()){
            int userID = rs.getInt("userID");
            Statement S, S1;
            S = conn.createStatement();
            S.executeUpdate("Delete from account "
                    + "where userID = " + userID);
            return 0;

            }
                  else
            JOptionPane.showMessageDialog( null, "Username does not Exist" );
                    } catch (SQLException ex) {
                        Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
                        return 1;
                    }
                 return 2;
                }

public ResultSet getlogin(String u, String p){
        ResultSet rs;

        try{

            ConnectToDatabase();
            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT userType, username from Account where username=" + "'" +u+ "'"+"and password="
                    +"'" +p+ "'");
        }

         catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return rs;
    }



        
        

    

 public ResultSet getusertype(String ut){
        ResultSet rs;

        try{

            ConnectToDatabase();
            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT userType, username from Account where userType=" + "'" +ut + "'");
        }

         catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return rs;
    }


public ResultSet getuser(String ut, String u){

        ResultSet rs;

        try{

            ConnectToDatabase();
            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * from account where userType=" + "'" +ut + "'"+"and username="
                    +"'" +u+ "'");
          

        }

         catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return rs;
    }

public ResultSet getuser3(String ut){

        ResultSet rs;

        try{

            ConnectToDatabase();
            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * from account where userType=" + "'" +ut + "'");


        }

         catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return rs;
    }

public ResultSet getuser4(){

        ResultSet rs;

        try{

            ConnectToDatabase();
            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * from account");


        }

         catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return rs;
    }



public ResultSet getuser1(String u){

        ResultSet rs;

        try{

            ConnectToDatabase();
            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * from account where username="
                    +"'" +u+ "'");

            System.out.println("SELECT * from account where username="
                    +"'" +u+ "'");
        }

         catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return rs;
    }


public void updateuser(int uid, String ut, String u, String p, String sq, String sa){

    
ResultSet rs;

        try{
            ConnectToDatabase();
            Statement stmt = conn.createStatement();
                  stmt.executeUpdate("Update account set username = " +"'" +u+ "'" +", userType="
                    +"'" +ut+ "'"+", password="
                    +"'" +p+ "'"+", securityQuestion="
                    +"'" +sq+ "'"+", securityAnswer="
                    +"'" +sa+ "'"+" where userID="
                    +"'" +uid+ "'"
                    );
                  

        }
        catch (Exception e){
            e.printStackTrace();
            }
}

   

}
